/*
 * Created on 8 mars 2005
 */
package clustering.implementations;
import clustering.framework.*;
//import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;

/**
 * @author Tudor.Ionescu@supelec.fr

HTMLPresentationEngine

This class creates a representation of an XML tree using HTML tables and different colors.

 */
public class HTMLPresentationEngine implements IPresentationEngine {
	String htmlHeader = "<html><body style=\"font-family: arial; font-size: 8pt;\"><table cellpadding=0 cellspacing=0><tr>";
	String htmlBody = "";
	String htmlFooter = "</tr></table></body></html>";
	String htmlContent = "";
	public void ParseAndDisplay(String sName, Document xmlTree, String [] fileNames, String outputFilePath)throws Exception{
		htmlContent = "<td><table cellpadding=0 cellspacing=1>";
		GenerateHTML(xmlTree.getFirstChild(),0,fileNames);
		htmlContent += "</table></td>";
		htmlBody += htmlContent;
		
		FileOutputStream fos = new FileOutputStream(outputFilePath);
		fos.write(htmlHeader.getBytes());
		fos.write(htmlBody.getBytes());
		fos.write(htmlFooter.getBytes());
		fos.close();		
	}
	void GenerateHTML(Node xmlNode, int depth,String [] fileNames){
		if(xmlNode.hasChildNodes()){
			// connection node
			String sDist = xmlNode.getAttributes().getNamedItem("dist").getNodeValue();
			sDist = sDist.substring(0,5);
			String sStep = xmlNode.getAttributes().getNamedItem("step").getNodeValue();
			htmlContent += "<tr><td style=\"font-size: 9pt; padding-left: "+(25*depth)+"px;\">";
			htmlContent += "<div style=\"border-left-width: 2px; border-left-style: solid; border-left-color: lightsteelblue; padding: 1px 3px 1px 3px; background-color: aliceblue;\"> +";
			htmlContent += " distance="+sDist+", step="+sStep+" </div>";
			htmlContent += "</tr></td>";
			NodeList nodeList = xmlNode.getChildNodes();
			for(int i=0;i<nodeList.getLength();i++){
				GenerateHTML(nodeList.item(i),depth+1,fileNames);
			}
		}else{
			// endnode
			String sId = xmlNode.getAttributes().getNamedItem("id").getNodeValue();
			int id = Integer.parseInt(sId);
			String sFileName = "";
			if(fileNames[id].indexOf("\\")!=-1){
				sFileName = fileNames[id].substring(fileNames[id].lastIndexOf("\\")+1);
			}else if(fileNames[id].indexOf("/")!=-1){
				sFileName = fileNames[id].substring(fileNames[id].lastIndexOf("/")+1);
			}else sFileName = fileNames[id];
			htmlContent += "<tr><td style=\"font-size: 9pt; padding-left: "+(25*depth)+"px;\">";
			htmlContent += "<div align=right style=\"border-left-width: 2px; border-left-style: solid; border-left-color: lightsteelblue; padding: 1px 3px 1px 3px; background-color: papayawhip;\">File: <b>"+sFileName+"</b></div>";
			htmlContent += "</tr></td>";
		}
	}
}
